import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { Card, Form, Table, Divider, Row, Col, Select, Button } from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import { Link } from 'dva/router';

import ScoreShowTable from '../../components/_public/ScoreShowTable';

import { getParamUrl } from '../../utils/formatDataSource';

@connect(({ lesson1, question, loading }) => ({
  lesson:lesson1,
  question,
  loading: loading.models.lesson1,
}))
class TestPaperScore extends PureComponent {
  componentDidMount() {
    const { dispatch, location: { search } } = this.props;

    const params = getParamUrl(search);

    dispatch({
      type: 'question/examInfo',
      payload: {
        ...params,
      },
    });

    dispatch({
      type: 'question/checkList1',
      payload: {
        ...params,
        questionType: 1,
        isHomework: 0,
      },
    });
    dispatch({
      type: 'question/checkList2',
      payload: {
        ...params,
        questionType: 2,
        isHomework: 0,
      },
    });
    dispatch({
      type: 'question/checkList3',
      payload: {
        ...params,
        questionType: 3,
        isHomework: 0,
      },
    });
  }

  getColumns = () => {
    return [
      {
        title: '学生',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: '选择题得分',
        dataIndex: 'xzScore',
        key: 'xzScore',
      },
      {
        title: '填空题得分',
        dataIndex: 'tkScore',
        key: 'tkScore',
      },
      {
        title: '简答题得分',
        dataIndex: 'jdScore',
        key: 'jdScore',
      },
      {
        title: '总分',
        dataIndex: 'totalScore',
        key: 'totalScore',
      },
    ];
  };

  handleChange = () => {};

  getDatasource = () => {
    const { xzData, tkData, jdData } = this.props.question;

    // {
    //   name: '张三',
    //     xzScore: 100,
    //   tkScore: 100,
    //   jdScore: 100,
    //   totalScore: 100,
    // }
    if (xzData.data.length === 0) {
      return;
    }
    let result = [];
    if (Object.keys(xzData).length > 0) {
      if (xzData.tableHeader !== null) {
        xzData.tableHeader.map(s => {
          result.push({
            id: s.id,
            name: s.name,
          });
        });
      }
    }

    result.map(r => {
      let xzScore = 0;
      let tkScore = 0;
      let jdScore = 0;
      if (Object.keys(xzData).length > 0) {
        xzScore = this.getScore(xzData.data, r.id);
      }
      if (Object.keys(tkData).length > 0) {
        tkScore = this.getScore(tkData.data, r.id);
      }
      if (Object.keys(jdData).length > 0) {
        jdScore = this.getScore(jdData.data, r.id);
      }

      const total = xzScore + tkScore + jdScore;
      r.xzScore = xzScore;
      r.tkScore = tkScore;
      r.jdScore = jdScore;
      r.totalScore = total;
    });
    return result;
  };

  getScore = (data, studentId) => {
    let score = 0;
    data.map(d => {
      const my = d.students.filter(s => s.id === studentId)[0];
      score += my.myScore === null ? 0 : my.myScore;
    });
    return score;
  };

  render() {
    const { xzData, tkData, jdData, examHeadInfo = {} } = this.props.question;

    return (
      <PageHeaderLayout>
        <Card bordered={false}>
          <Row>
            <Col span={4}>
              试卷名称：<strong>{examHeadInfo.examName}</strong>
            </Col>
            <Col span={6}>
              科目：<strong>{examHeadInfo.subject}</strong>
            </Col>
            <Col span={4}>
              所属课程：<strong>{examHeadInfo.lessonName}</strong>
            </Col>
            <Col span={4}>
              状态：<strong>{examHeadInfo.lessonStatus}</strong>
            </Col>
          </Row>
        </Card>

        <Card bordered={false} title="得分">
          <Table
            columns={this.getColumns()}
            dataSource={this.getDatasource()}
            onChange={this.handleChange}
            size="small"
            bordered={true}
          />
        </Card>
        <Card bordered={false} title="选择题">
          <ScoreShowTable dataSource={xzData} questionType={1} lessonStatus={1} />
        </Card>
        <Card bordered={false} title="填空题">
          <ScoreShowTable dataSource={tkData} questionType={2} lessonStatus={1} />
        </Card>
        <Card bordered={false} title="简答题">
          <ScoreShowTable dataSource={jdData} questionType={3} lessonStatus={1} />
        </Card>
      </PageHeaderLayout>
    );
  }
}

export default TestPaperScore;
